package com.hdax.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.hdax.common.exception.BizCodeEnume;
import com.hdax.gulimall.member.exception.PhoenNameExistExeception;
import com.hdax.gulimall.member.exception.UserNameExistExeception;
import com.hdax.gulimall.member.feign.CouponFeignService;
import com.hdax.gulimall.member.vo.MemberLoginVo;
import com.hdax.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.hdax.gulimall.member.entity.MemberEntity;
import com.hdax.gulimall.member.service.MemberService;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 会员
 *
 * @author xiaohou
 * @email 2687781461@qq.com
 * @date 2023-04-08 23:27:37
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @Autowired
    private CouponFeignService couponFeignService;


    /**
     * MD5加密测试
     */
    @RequestMapping("/jiami")
    public void jiami(){
        //普通的加密
//        String s= DigestUtils.md5DigestAsHex("123455".getBytes());
//        System.out.println(s);
//        //盐值加密
//        String s1 = Md5Crypt.md5Crypt("123456".getBytes());
//        System.out.println(s1);
        //每次加密的值都不一样
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        //$2a$10$I.zF70VmyaIQAKWvklBAy.oOVIyZEpggiiiuwirLZCoVOyydhH4Hy
        String encode = bCryptPasswordEncoder.encode("1234556");
        System.out.println(encode);
        //铭文的解密
        boolean matches = bCryptPasswordEncoder.matches("1234556", "$2a$10$I.123.oOVIyZEpggiiiuwirLZCoVOyydhH4Hy");
        System.out.println(matches);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo,HttpSession session){
        MemberEntity memberEntity=memberService.login(memberLoginVo);
        if(memberEntity!=null){
            //登录成功
            return R.ok().put("data", memberEntity);
        }else {
            //登录失败
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD.getCode(),BizCodeEnume.LOGINACCT_PASSWORD.getMsg());
        }
    }

    /**
     * 会员的注册
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo){
        try {
            memberService.regist(memberRegistVo);
        }catch (PhoenNameExistExeception e){
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistExeception e){
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    @RequestMapping("/coupons")
    public R test(){
        R coupon = couponFeignService.coupon();
        return coupon;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
